#include "..\include\CD3DWrapper.h"


	
CD3DWrapper::CD3DWrapper()
{
	
}

CD3DWrapper::~CD3DWrapper()
{
	
}

//===========================================================
// Method	:	GetInstance()
// Purpose	:	Return the single instance of our D3DWrapper
// Return	:	Our single instance
//===========================================================
CD3DWrapper* CD3DWrapper::GetInstance(void)
{
	// Create object when called for the first time.
	static CD3DWrapper instance;
	
	// Subsequent calls will return the same static instance.
	return &instance;
}

//===========================================================
// Method	:	InitializeD3D()
// Purpose	:	Attempt to initialize Direct3D
//
// In		:	hWnd - Handle to our window
//				nScreenWidth - Width of our screen
//				nScreenHeight - Height of our screen
//              bWindowed - false for Fullscreen mode
//
// Return	:	Return true if successful
//===========================================================
bool CD3DWrapper::InitializeD3D(HWND hWnd, int nScreenWidth, int nScreenHeight, bool bWindowed)
{
	// Guarantees the app is built against the correct headers.
	m_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
	
	D3DCAPS9 caps;
	m_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,
						  D3DDEVTYPE_HAL,
						  &caps);
						  
	// Can we user hardware vertex processing?
	int vp = 0;
	if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
		vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
	else
		vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
		
	D3DPRESENT_PARAMETERS d3dpp;
	d3dpp.BackBufferWidth				= nScreenWidth;
	d3dpp.BackBufferHeight				= nScreenHeight;
	d3dpp.BackBufferFormat				= D3DFMT_A8R8G8B8;
	d3dpp.BackBufferCount				= 1;
	d3dpp.MultiSampleType				= D3DMULTISAMPLE_NONE;
	d3dpp.MultiSampleQuality			= 0;
	d3dpp.SwapEffect					= D3DSWAPEFFECT_DISCARD;
	d3dpp.hDeviceWindow					= hWnd;
	d3dpp.Windowed						= bWindowed;
	d3dpp.EnableAutoDepthStencil		= true;
	d3dpp.AutoDepthStencilFormat		= D3DFMT_D24S8;
	d3dpp.Flags							= 0;
	d3dpp.FullScreen_RefreshRateInHz	= D3DPRESENT_RATE_DEFAULT;
	d3dpp.PresentationInterval			= D3DPRESENT_INTERVAL_IMMEDIATE;
	
	// Attempt to create our device
	HRESULT hr = 0;
	hr = m_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
							  D3DDEVTYPE_HAL,
							  hWnd,
							  vp,
							  &d3dpp,
							  &m_pD3DDevice);
							  
	if(FAILED(hr))
	{
		MessageBox(hWnd, "CreateDevice() - Failed", "Error", MB_OK);
		return false;
	}
	
	// Success!
	return true;
}

//===========================================================
// Method	:	Clear()
// Purpose	:	Clear the screen to a specified color
//
// In		:	RGB colors for the screen
//===========================================================
void CD3DWrapper::Clear(unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue)
{
	HRESULT hr = 0;
	hr = m_pD3DDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(ucRed, ucGreen, ucBlue), 1.0f, 0);
}
